package com.clouds.wms.util;



import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class ExcelImportChecker implements XmlExcelChecker {

	private static Logger log = Logger.getLogger(ExcelImportChecker.class);
	private String checkerXmlFile  ;
	//
	private static Document document = null;
	private static Node cellNode = null;
	private static Element cellElement = null;
	private static Element dateTypeElement = null;
	public ExcelImportChecker(String checkerXmlFile){
		this.checkerXmlFile = checkerXmlFile;
	}
	@Override
	public void parseXml() {
		try {
			SAXReader saxReader = new SAXReader();
			InputStream is = this.getClass().getResourceAsStream(
					checkerXmlFile);
			document = saxReader.read(is);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	

	private static List<Integer> notNullList = null;
	@Override
	public String rowCheck(Row row) {
		String errorMsg = null;
		// 不能为空的字段
		if(notNullList==null)
		{
			notNullList = new ArrayList<Integer>();
			String notNullCell = this.getCellElement().element("notNull").getTextTrim();
			String[] strArr = notNullCell.split(",");
			
			for(String str:strArr)
			{
				notNullList.add(Integer.parseInt(str.trim()));
			}
		}
		for(int i:notNullList)
		{
			Cell cell = row.getCell(i);
			if(cell==null)
			{
				errorMsg = "第"+(row.getRowNum()+1)+"行，第"+(i+1)+"列，不能为空值。";
				break;
			}else
			{
				String cellvalue = cell.toString();
				cellvalue = cellvalue==null?"":cellvalue.trim();
				if(cellvalue.length()==0)
				{
					errorMsg = "第"+(row.getRowNum()+2)+"行，第"+(i+1)+"列，不能为空值。";
					break;
				}
			}
		}
		return errorMsg;
	}
	@Override
	public String cellCheck(Cell cell)
	{
		String errorMsg = null;
		// 不能为空的字段
		if(notNullList==null)
		{
			notNullList = new ArrayList<Integer>();
			String notNullCell = this.getCellElement().element("notNull").getTextTrim();
			String[] strArr = notNullCell.split(",");
			
			for(String str:strArr)
			{
				notNullList.add(new Integer(str.trim()));
			}
		}
		String strValue = cell.getStringCellValue();
		strValue = strValue==null?"":strValue.trim();
		if(notNullList.contains(cell.getColumnIndex())&&strValue.length()>0)
		{
			errorMsg = "第"+(cell.getRowIndex()+1)+"行，第"+(cell.getColumnIndex()+1)+"列，不能为空值。";
		}
		return errorMsg;
	}

	@Override
	public int getCellType(int col) {
		int retInt = -1;
		Element colElement = this.getDataTypeElement().element("c"+col);
		if(colElement!=null)
		{
			retInt = Integer.parseInt(colElement.getTextTrim());
		}
		return retInt;
	}
	private Document getDoc()
	{
		if(document==null)
		{
			parseXml();
		}
		return document;
	}
	private Node getCellNode()
	{
		if(cellNode==null)
		{
			cellNode = this.getDoc().selectSingleNode("//cell");
		}
		return cellNode;
	}
	private Element getCellElement()
	{
		if(cellElement==null)
		{
			Element root = this.getDoc().getRootElement();
			Element row = root.element("row");
			cellElement = row.element("cell");
		}
		return cellElement;
	}
	private Element getDataTypeElement()
	{
		if(dateTypeElement==null)
		{
			dateTypeElement = getCellElement().element("dataType");
		}
		return dateTypeElement;
	}
}
